package gui;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

import database.UserOperations;

public class forgotPasswordGUI extends JFrame implements ActionListener,ItemListener 
{
	private static final long serialVersionUID = 1L;
	private JPanel p1, p2, p3;
	private JLabel l1,l3,l4,l5,l6,l7,question;
	private JTextField f1,f3,f4;
	private JButton login, cancel;
	private JComboBox <String> box;
	private ArrayList<String> names;
	private String usrName;
	private UserOperations uo;
	
	public forgotPasswordGUI() 
	{
		super("Forgot Login Details");
		this.setSize(470, 360);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		uo = new UserOperations();

		//P1
		p1 = new JPanel(new BorderLayout());
		l1 = new JLabel("Not to worry, just answer the security question below.");
		p1.add(Box.createRigidArea(new Dimension(0,50)));
		p1.add(l1, BorderLayout.NORTH);
		
		//P2
		p2 = new JPanel(new GridLayout(5, 1));
		p2.setPreferredSize(new Dimension(250, 100));
		l3 = new JLabel("Answer: ");
		f1 = new JTextField(15);
		l4 = new JLabel("Username: ");
		
		names = uo.queryUserNames();
		box = new JComboBox<String>();
		for (int i=0;i<names.size();i++)
		{
			box.addItem(names.get(i));	
		}
		box.addItemListener(this);
		box.setSelectedIndex(0);
		usrName = (String)box.getSelectedItem();
		
		l5 = new JLabel("New Password: ");
		l6 = new JLabel("Re-Type Password: ");
		f3 = new JTextField(15);
		f4 = new JTextField(15);
		l7 = new JLabel("Question:");
		uo.getSecQ(usrName);
	    String quest = uo.getSecQ((String)box.getSelectedItem());
		question = new JLabel(quest);
		p2.add(l4);
		p2.add(box);
		p2.add(l7);
		p2.add(question);
		p2.add(l3);
		p2.add(f1);
		p2.add(l5);
		p2.add(f3);
		p2.add(l6);
		p2.add(f4);
		
		//P3
		p3 = new JPanel();
		p3.setPreferredSize(new Dimension(470 ,100));
		login = new JButton("LOGIN");
		login.addActionListener(this);
		cancel = new JButton("CANCEL");
		cancel.addActionListener(this);
		
		p3.add(login);
		p3.add(Box.createRigidArea(new Dimension(150,110)));
		p3.add(cancel);
		
		//Adding Panels
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.CENTER);
		this.add(p3, BorderLayout.PAGE_END);
	}
	
	public static void main(String args[]) 
	{
		forgotPasswordGUI fp = new forgotPasswordGUI();
		fp.setVisible(true);
	}
	
	public void updatePass()
	{
		String password = f3.getText();
		String rePassword = f4.getText();
		String answer = f1.getText();
		String ans = uo.updatePassword(password,usrName);
		if (!password.equals(rePassword))
		{
			JOptionPane.showMessageDialog(null,
					"Passwords do not match");
		}
		else if (!answer.equals(ans))
		{
			JOptionPane.showMessageDialog(null,
					"Incorrect Answer!");
		}
		else 
		{
			uo.updatePassword(password,usrName);
			JOptionPane.showMessageDialog(null,"Password has been updated");
			this.setVisible(true);
		}
	}

	@Override
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==login)
		{
			updatePass();
			this.dispose();
		}
		if(ae.getSource()==cancel)
		{
			this.dispose();
		}
		
	}

	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
		String str = (String) box.getSelectedItem();
		String questionx = uo.getSecQ(str);
		question.setText(questionx);
		usrName=(String)box.getSelectedItem();
		//String quest=questionx;
	}

}